package com.${companyName}.${projectName}.service.${model}.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.${companyName}.${projectName}.dao.${model}.I${objectName}Dao;
import com.${companyName}.${projectName}.model.${model}.${objectName};
import com.${companyName}.${projectName}.service.${model}.I${objectName}Service;
import com.${companyName}.${projectName}.tools.common.UUIDGenerator;
import com.${companyName}.${projectName}.tools.pager.PagerModel;
import com.${companyName}.${projectName}.tools.pager.Query;


<#assign primaryKeyTypeJava="String"/> 
<#if columnFieldList??>  
	<#list columnFieldList as item>
		<#if item.column=='${primaryKeys[0]}' && item.jdbcType=='INTEGER'>
			<#assign primaryKeyTypeJava="Integer"/>
		</#if>
	</#list>
</#if>


/**
 * ${objectNameCn}Service实现
 * @author ${author}
 * @date ${createTime}
 */
@Service
public class ${objectName}ServiceImpl implements I${objectName}Service {

	@Resource
	private I${objectName}Dao ${fLowerObjectName}Dao;

	@Override
	public ${objectName} get${objectName}By${primaryKeyMerger}(${primaryKeyTypeJava} ${primaryKeysConvert[0]}) throws Exception {
		<#if primaryKeyTypeJava="String">
		return StringUtils.isNotBlank(${primaryKeysConvert[0]}) ? this.${fLowerObjectName}Dao.get${objectName}By${primaryKeyMerger}(${primaryKeysConvert[0]}.trim()) : null;
		<#elseif primaryKeyTypeJava="Integer">
		return null!=${primaryKeysConvert[0]} ? this.${fLowerObjectName}Dao.get${objectName}By${primaryKeyMerger}(${primaryKeysConvert[0]}) : null;
		</#if>
	}

	@Override
	public List<${objectName}> get${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s) throws Exception {
		${primaryKeysConvert[0]}s = StringTools.converString(${primaryKeysConvert[0]}s);
		return StringUtils.isNotBlank(${primaryKeysConvert[0]}s) ? this.${fLowerObjectName}Dao.get${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s) : null;
	}
	
	@Override
	public List<${objectName}> get${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s) throws Exception {
		return CollectionUtils.isNotEmpty(${primaryKeysConvert[0]}s) ? this.${fLowerObjectName}Dao.get${objectName}By${primaryKeyMerger}sList(${primaryKeysConvert[0]}s) : null;
	}

	@Override
	public List<${objectName}> getAll(${objectName} ${fLowerObjectName}) throws Exception {
		return null != ${fLowerObjectName} ? this.${fLowerObjectName}Dao.getAll(${fLowerObjectName}) : null;
	}

	@Override
	public PagerModel<${objectName}> getPagerModelByQuery(${objectName} ${fLowerObjectName}, Query query)
			throws Exception {
		return (null != ${fLowerObjectName} && null != query) ? this.${fLowerObjectName}Dao.getPagerModelByQuery(${fLowerObjectName}, query) : null;
	}
	
	@Override
	public int getByPageCount(${objectName} ${fLowerObjectName})throws Exception {
		return (null != ${fLowerObjectName}) ? this.${fLowerObjectName}Dao.getByPageCount(${fLowerObjectName}) : 0;
	}

	@Override
	public void insert${objectName}(${objectName} ${fLowerObjectName}) throws Exception {
		this.${fLowerObjectName}Dao.insert${objectName}(${fLowerObjectName});
	}
	
	@Override
	public void insert${objectName}Batch(List<${objectName}> ${fLowerObjectName}s) throws Exception {
		this.${fLowerObjectName}Dao.insert${objectName}Batch(${fLowerObjectName}s);
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}(${primaryKeyTypeJava} ${primaryKeysConvert[0]}) throws Exception {
		<#if primaryKeyTypeJava="String">
		if (StringUtils.isNotBlank(${primaryKeysConvert[0]})) {
			this.${fLowerObjectName}Dao.del${objectName}By${primaryKeyMerger}(${primaryKeysConvert[0]}.trim());
		}
		<#elseif primaryKeyTypeJava="Integer">
		if (null!=${primaryKeysConvert[0]}) {
			this.${fLowerObjectName}Dao.del${objectName}By${primaryKeyMerger}(${primaryKeysConvert[0]});
		}
		</#if>		
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s) throws Exception {
		${primaryKeysConvert[0]}s = StringTools.converString(${primaryKeysConvert[0]}s);
		if(StringUtils.isNotBlank(${primaryKeysConvert[0]}s)){
			this.${fLowerObjectName}Dao.del${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s);
		}
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s) throws Exception {
		if(CollectionUtils.isNotEmpty(${primaryKeysConvert[0]}s)){
			this.${fLowerObjectName}Dao.del${objectName}By${primaryKeyMerger}sList(${primaryKeysConvert[0]}s);
		}
	}

	@Override
	public int update${objectName}(${objectName} ${fLowerObjectName}) throws Exception {
		return this.${fLowerObjectName}Dao.update${objectName}(${fLowerObjectName});
	}
	
	@Override
	public int update${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s,${objectName} ${fLowerObjectName}) throws Exception {
		return this.${fLowerObjectName}Dao.update${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s, ${fLowerObjectName});
	}
	
	@Override
	public int update${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s,${objectName} ${fLowerObjectName}) throws Exception {
		return this.${fLowerObjectName}Dao.update${objectName}By${primaryKeyMerger}sList(${primaryKeysConvert[0]}s, ${fLowerObjectName});
	}
	
	@Override
	public int update${objectName}List(List<${objectName}> ${fLowerObjectName}s) throws Exception {
		return this.${fLowerObjectName}Dao.update${objectName}List(${fLowerObjectName}s);
	}
	
	/***
	 * 将"1,2,3,4,5..."这种形式的字符串转成<#if primaryKeyTypeJava="String">"'1','2','3','4'..."<#elseif primaryKeyTypeJava="Integer">"1,2,3,4..."</#if>这种形式
	 * @param strs
	 * @return
	 
	private String converString(String strs) {
		if (StringUtils.isNotBlank(strs)) {
			String[] idStrs = strs.trim().split(",");
			if (null != idStrs && idStrs.length > 0) {
				StringBuffer sbf = new StringBuffer("");
				for (String str : idStrs) {
					if (StringUtils.isNotBlank(str)) {
						<#if primaryKeyTypeJava="String">
						sbf.append("'").append(str.trim()).append("'").append(",");
						<#elseif primaryKeyTypeJava="Integer">
						sbf.append(str.trim()).append(",");
						</#if>
					}
				}
				if (sbf.length() > 0) {
					sbf = sbf.deleteCharAt(sbf.length() - 1);
					return sbf.toString();
				}
			}
		}
		return "";
	}*/
	
	/***
	 * 将"1,2,3,4,5..."这种形式的字符串转成List<String> 集合
	 * 
	 * @param strs
	 * @return
	 
	private List<String> converStringToList(String strs) {
		if (StringUtils.isNotBlank(strs)) {
			String[] idStrs = strs.trim().split(",");
			if (null != idStrs && idStrs.length > 0) {
				List<String> strsList = new ArrayList<String>();
				for (String str : idStrs) {
					if (StringUtils.isNotBlank(str)) {
						strsList.add(str.trim());
					}
				}
				if (strsList.size() > 0) {
					return strsList;
				}
			}
		}
		return null;
	}
	*/
	
	//------------api------------
	
}

